// pages/cart/cart.js
const api = require('../../api/index')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        count: 0,
        amount: 0,
        saleShopcarts: [],
        address: {},
        isLogin: false
    },
    async submitOrder() {
        this.data.saleShopcarts.forEach(item => {
            delete item.id
            delete item.createTime
            delete item.updateTime
        })
        const order = {
            addressId: this.data.address.id,
            amount: this.data.amount,
            orderDetails: this.data.saleShopcarts,
            status: -1,
            userId: wx.getStorageSync('token').id
        }
        await api.cart.submitOder(order)
        this.getCartList()
        wx.showToast({
            title: '订单提交成功！',
            type: 'success'
          })
        wx.navigateTo({
          url: '/pages/order/order?status=' + -2,
        })
    },
    removeCartItem(e) {
        wx.showModal({
          title: '提示',
          content: '确定删除该商品吗?',
          complete: async (res) => {
            if (res.confirm) {
                const id = e.currentTarget.dataset.id
                await api.cart.remove(id)
                this.getCartList()
            }
          }
        })
        wx.showToast({
          title: '删除成功！',
          type: 'success'
        })
    },
    async getCartList() {
        const {id} = wx.getStorageSync('token')
        const { saleShopcarts, count, amount } = await api.cart.cartList(id)
        this.setData({ saleShopcarts, count, amount })
    },
    async addChange(e) {
        const id = e.currentTarget.dataset.id
        const value = e.currentTarget.dataset.count + 1
        await api.cart.countChange({id, value})
        this.getCartList()
    },
    async rdcChange(e) {
        const id = e.currentTarget.dataset.id
        const value = e.currentTarget.dataset.count - 1
        await api.cart.countChange({id, value})
        this.getCartList()
    },
    
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const userInfo = wx.getStorageSync('token')
        if(userInfo === "") {
            this.setData({ isLogin: true })
            this.setData({ saleShopcarts: [], count: 0, amount: 0 })
        } else {
            this.setData({ isLogin: false })
            this.getCartList()
        }
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const { id } = wx.getStorageSync('token')
        const cid = options.cid
        if(cid === undefined) {
            const addressList = await api.address.addressList(id)
            this.setData({ address: addressList[0] })
        } else {
            const address = await api.address.getModel(cid)
            this.setData({ address })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})